package com.orangelife.mobile.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.confirmOrder.adapter.GoodsDispatchListViewAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商品配送方式弹出框
 * Created by cfr on 2015/10/10.
 */
public class GoodsDispatchPopupWindow extends PopupWindow {
    private Context context;
    private List<String> data;
    public static String[] arrTitle;

    private ListView lvType;
    private TextView tvTitle;
    public OnGoodsPopupWindowItemClickListener listener;
    private GoodsDispatchListViewAdapter typeAdapter;
    /**
     * 接收传过来的数据
     */
    private List<Map<String, Object>> listType;

    /**
     * 构造方法
     */
    public GoodsDispatchPopupWindow(Context context, List<Map<String, Object>> list) {
        this.context = context;
        this.listType = list;
        initData();
        initObject(context);
    }


    /**
     * 当点击item时设置标题的值
     *
     * @param tvTitle
     */
    public void setText(TextView tvTitle) {
        this.tvTitle = tvTitle;
    }

    /**
     * 设置数据
     */
    private void initData() {
//        arrTitle = context.getResources().getStringArray(
//                R.array.freshInfor_title);
        data = new ArrayList<String>();
        getShopInforCondition();
    }

    /**
     * 搜索条件
     */
    private void getShopInforCondition() {
        if (listType != null) {
            for (int i = 0; i < listType.size(); i++) {
                data.add(listType.get(i).get("dispatchName").toString());
            }
        }
    }

    /**
     * 初始化popupwindow
     *
     * @param context
     */
    private void initObject(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
        View contentView = LayoutInflater.from(context).inflate(
                R.layout.popwin_choose_dispatching, null);
        setContentView(contentView);
        /* 设置popupwindow的基本属性 */
        lvType = (ListView) contentView.findViewById(R.id.lvDispatching);
        this.setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(R.color.white)));
        this.setFocusable(true);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
		/* 设置捕获popupwindow外的点击焦点 */
        this.setOutsideTouchable(true);

        setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    dismiss();
                    return true;
                }
                return false;
            }
        });
        setAdapter();
        // setListener();
    }

    /**
     * 载入列表item
     */
    private void setAdapter() {
        typeAdapter = new GoodsDispatchListViewAdapter(context, data);
        lvType.setAdapter(typeAdapter);
    }

    /**
     * 设置popupwindow的适配器
     */
    public void setOnGoodsPopupWindowClickListener(
            OnGoodsPopupWindowItemClickListener listener) {
        this.listener = listener;
        typeAdapter.setListener(listener);
    }

    /**
     * 为列表的item添加监听
     */
    public interface OnGoodsPopupWindowItemClickListener {
        void onPopupWindowItemClick(int clickPositon, String strItemData);
    }

}
